package com.dawn.module.hospital.controller.admin.qrcodepay;

import com.dawn.framework.apilog.core.annotation.ApiAccessLog;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.framework.excel.core.util.ExcelUtils;
import com.dawn.module.hospital.controller.admin.qrcodepay.vo.QrcodePayPageReqVO;
import com.dawn.module.hospital.controller.admin.qrcodepay.vo.QrcodePayRefundReqVO;
import com.dawn.module.hospital.controller.admin.qrcodepay.vo.QrcodePayRespVO;
import com.dawn.module.hospital.controller.admin.qrcodepay.vo.QrcodePaySaveReqVO;
import com.dawn.module.hospital.dal.dataobject.qrcodepay.QrcodePayDO;
import com.dawn.module.hospital.service.qrcodepay.QrcodePayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.dawn.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.dawn.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 扫码支付")
@RestController
@RequestMapping("/hospital/qrcode-pay")
@Validated
public class QrcodePayController {

    @Resource
    private QrcodePayService qrcodePayService;

    @PostMapping("/create")
    @Operation(summary = "创建扫码支付")
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:create')")
    public CommonResult<Long> createQrcodePay(@Valid @RequestBody QrcodePaySaveReqVO createReqVO) {
        return success(qrcodePayService.createQrcodePay(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新扫码支付")
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:update')")
    public CommonResult<Boolean> updateQrcodePay(@Valid @RequestBody QrcodePaySaveReqVO updateReqVO) {
        qrcodePayService.updateQrcodePay(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除扫码支付")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:delete')")
    public CommonResult<Boolean> deleteQrcodePay(@RequestParam("id") Long id) {
        qrcodePayService.deleteQrcodePay(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得扫码支付")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:query')")
    public CommonResult<QrcodePayRespVO> getQrcodePay(@RequestParam("id") Long id) {
        QrcodePayDO qrcodePay = qrcodePayService.getQrcodePay(id);
        return success(BeanUtils.toBean(qrcodePay, QrcodePayRespVO.class));
    }

    @GetMapping("/query")
    @Operation(summary = "扫码支付查询支付状态")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:query')")
    public CommonResult queryQrcodePayStatus(@RequestParam("id") Long id) {
        CommonResult result = qrcodePayService.queryQrcodePayStatus(id);
        return result;
    }


    @PostMapping("/refund")
    @Operation(summary = "扫码支付退款")
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:refund')")
    public CommonResult<Long> createQrcodePay(@Valid @RequestBody QrcodePayRefundReqVO reqVO) {
        CommonResult result = qrcodePayService.refundQrcodePay(reqVO);
        return result;
    }

    @GetMapping("/page")
    @Operation(summary = "获得扫码支付分页")
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:query')")
    public CommonResult<PageResult<QrcodePayRespVO>> getQrcodePayPage(@Valid QrcodePayPageReqVO pageReqVO) {
        PageResult<QrcodePayDO> pageResult = qrcodePayService.getQrcodePayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QrcodePayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出扫码支付 Excel")
    @PreAuthorize("@ss.hasPermission('hospital:qrcode-pay:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQrcodePayExcel(@Valid QrcodePayPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QrcodePayDO> list = qrcodePayService.getQrcodePayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "扫码支付.xls", "数据", QrcodePayRespVO.class,
                        BeanUtils.toBean(list, QrcodePayRespVO.class));
    }

}